from django.shortcuts import render

# Create your views here.
from rest_framework import request
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from areas.Serializers import AreasSerializer, AreasCitySerializer
from areas.models import Areas

# # 获取省级信息
# # GET /areas/
# class AreasView(ListAPIView):
#     queryset = Areas.objects.filter(parent_id=None)
#     serializer_class = AreasSerializer
#
# #　获取省级以下地区信息
# # GET /areas/(?P<pk>\d+)/
# class AreasCityView(RetrieveAPIView):
#     queryset = Areas.objects.all()
#     serializer_class = AreasCitySerializer

# 地区信息获取优化
class AreaViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    pagination_class = None  # 区划信息不分页
    def get_serializer_class(self):
        if self.action == 'list':
            return AreasSerializer
        else:
            return AreasCitySerializer

    def get_queryset(self):
        if self.action == 'list':
            return Areas.objects.filter(parent_id=None)
        else:
            return Areas.objects.all()

